package com.authine.cloudpivot.ext.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;

import java.lang.reflect.Type;

/**
 * @Author hxd
 * @Date 2021/10/28 14:46
 * @Description 前置网关过来的参数, 需要把query里数据反序列化到对象里, 有数组的字段当只有一个值是默认是String, 解析成 JSONArray
 **/
public class ObjToCollection implements ObjectDeserializer {
    @Override
    public <T> T deserialze(final DefaultJSONParser defaultJSONParser, final Type type, final Object o) {
        Object value = defaultJSONParser.parse();
        if (value instanceof String) {
            JSONArray objects = new JSONArray();
            objects.add(value);
            return (T) objects;
        }
        return (T) value;
    }

    @Override
    public int getFastMatchToken() {
        return 0;
    }
}
